#!/usr/bin/env python
# -*- coding:utf-8 -*-
from lxml import etree

from core.parser import BaseParser


class Parser(BaseParser):
    use_db = []
    URL = ''
    NAME = ''

    def run(self, index, end=None):
        url = self.URL % (index,)
        try:
            ret, _ = self.http_get(url)
        except Exception as e:
            self.log.error('run|http_get error url->%s', url, exc_info=1)
            if end and end > index+1:
                self.msg_que.put((
                    self.NAME,
                    (index+1, end),
                    self.msg_que,
                    self.options
                ))
            return
        xpath_list = ret.xpath('//div[@class=\'list-con\']/ul/li')
        for xpath_item in xpath_list:
            # 提前屏蔽非同花顺文章
            x = xpath_item.xpath('./a[@class=\'arc-cont\']/text()')
            if len(x) <= 0:
                self.log.error('run|class="arc-cont" find error, url->%s|html->%s', url,
                               etree.tostring(ret, method='html', encoding='utf-8'))
            else:
                if x[0].strip() == '...':
                    continue

            # 文章url
            x = xpath_item.xpath('./span[@class=\'arc-title\']/a[@title]/@href')
            if len(x) > 0:
                article_url = x[0].strip()
            else:
                self.log.error('run|not find title, url->%s|html->%s', url,
                               etree.tostring(xpath_item, method='html', encoding='utf-8'))
                continue

            #
            x = xpath_item.xpath('./span[@class=\'arc-title\']/a[@title]/@title')
            if len(x) > 0:
                title = x[0].strip()
            else:
                self.log.warning('run|not find title, url->%s', url)
                title = None

            self.msg_que.put((
                'a10jqka_article',
                (article_url, title)
            ))

        if end is None:
            x = ret.xpath('//span[@class=\'num-container\']/a[@class=\'end\']/text()')
            if len(x) > 0:
                end = int(x[0].strip())
            else:
                self.log.error('run|not find end index, url->%s', url)

        # 添加下一个抓取页
        if end > index:
            self.msg_que.put((
                self.NAME,
                (index+1, end)
            ))
